home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / utility.lha / utility / statcount.C < prev    next >
C/C++ Source or Header  |  1993-08-08  |  795b  |  43 lines

  1. // $Id: statcount.C,v 1.1 91/09/06 12:06:34 dag Exp $
  2.  
  3.  
  4. #include <statcount.H>
  5. #include <stdio.h>
  6. #include <strings.h>
  7.  
  8.  
  9. StatCount :: StatCount(const char* s, int mini, int maxi, unsigned reso)
  10.     : min(mini), res(reso), n((maxi - mini)/res), v(new unsigned[n])
  11. {
  12.   if (s == 0) s = "";
  13.   title = new char[strlen(s) + 1];
  14.   strcpy(title, s);
  15.   
  16.   for (int i = 0; i < n; i++)
  17.     v[i] = 0;
  18. }
  19.  
  20. void StatCount :: operator () (int x)
  21. {
  22.   register int i = (x - min) / res;
  23.   if (i < 0) i = 0;
  24.   if (i >= n) i = n-1;
  25.   v[i]++;
  26. }
  27.  
  28. void StatCount :: Dump() const
  29. {
  30.   fprintf(stderr, "%s\n", title);
  31.   for (int i = 0; i < n; i++) {
  32.     int start = i*res + min;
  33.     fprintf(stderr, "%d-%d\t%u\n", start, start+res-1, v[i]);
  34.   }
  35. }
  36.  
  37. StatCount :: ~StatCount()
  38. {
  39.   Dump();
  40.   delete [] title;
  41.   delete [] v;
  42. }
  43.